package com.example.myapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.base.Constant;
import com.example.myapplication.bean.MainHomeSpecialBean;

import java.util.ArrayList;

public class HomeMainSpecialAdapter extends RecyclerView.Adapter<HomeMainSpecialAdapter.ViewHolder> {

    private final ArrayList<MainHomeSpecialBean.RowsDTO> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public HomeMainSpecialAdapter(ArrayList<MainHomeSpecialBean.RowsDTO> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MainHomeSpecialBean.RowsDTO bean = arrayList.get(position);
        holder.mainTextView.setText("    " + bean.getTitle());
        Glide.with(holder.mainImageView).load(Constant.URL + bean.getCover()).into(holder.mainImageView);

        holder.mainCardView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(position, bean);
            }
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {

        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_home_main_special, group, false);
        return new ViewHolder(view);

    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public interface OnItemClickListener {

        @SuppressWarnings({"EmptyMethod"})
        void onClick(int position, MainHomeSpecialBean.RowsDTO bean);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final CardView mainCardView;
        private final AppCompatImageView mainImageView;
        private final AppCompatTextView mainTextView;

        private ViewHolder(View view) {

            super(view);
            mainCardView = view.findViewById(R.id.mainCardView);
            mainImageView = view.findViewById(R.id.mainImageView);
            mainTextView = view.findViewById(R.id.mainTextView);

        }

    }

}
